package cn.melon.mod.store;

import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import cn.melon.common.ModelType;
import cn.melon.helper.IOnRequestListener;
import cn.melon.helper.LoggerHelper;
import cn.melon.helper.RequestHelper;
import cn.melon.mod.store.entity.Goods;
import cn.melon.mod.store.entity.GoodsTable;
import cn.melon.view.admin.store.UpdateGoodsTable;
import cn.melon.window.MainWindow;

public class StoreServiceImpl implements StoreService {

    private String TAG = LoggerHelper.makeLogTag(StoreServiceImpl.class);
    private Logger storeServiceLog = LoggerHelper.clientLogger();
    
    /* (non-Javadoc)
     * @see cn.melon.mod.store.StoreService#getGoodsTable(cn.melon.view.admin.store.updateGoodsTable, java.lang.String, int, int, java.lang.String, java.lang.String)
     */
    @Override
    public boolean getGoodsTable(UpdateGoodsTable updateView,String type, int start, int num, String goodsId, String goodsName) {
        JSONObject jsonReq = new JSONObject();
        try {
            jsonReq.put("action", "ASKGOODSTABLE");
            jsonReq.put("GoodsType", type);
            jsonReq.put("start", start);
            jsonReq.put("num", num);
            jsonReq.put("GoodsId", goodsId);
            jsonReq.put("GoodsName", goodsName);
            storeServiceLog.info(jsonReq);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        new RequestHelper(ModelType.STORE).addRequest(jsonReq.toString())
                .addReceiver("goodsTable", new IOnRequestListener() {

                    @Override
                    public void onSuccess(String response) {
                        storeServiceLog.info(response);
                        try {
                            storeServiceLog.info(response);
                            JSONObject jRet = new JSONObject(response);
                            if(jRet.getInt("code") == 200) {
                                // 工作暂时到此, 要求每个请求均可以做到返回对象
                               JSONArray jsonArray = jRet.getJSONArray("GoodsTable");
                               for(int i=0;i<jsonArray.length();i++){
                                   storeServiceLog.info(jsonArray.get(i));
                               }
                               GoodsTable goodsTable = new GoodsTable();
                               goodsTable.setGoodsTable(jsonArray);
                               updateView.updateTable(goodsTable);
                            } else {
                                // 错误处理
                                storeServiceLog.info(TAG + "ERROR");
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        
                    }
                }).build().start();
        return true;
    }
    
    @Override
    public boolean getGoodsTable(MainWindow updateView,String type, int start, int num, String goodsId, String goodsName) {
        JSONObject jsonReq = new JSONObject();
        try {
            jsonReq.put("action", "ASKGOODSTABLE");
            jsonReq.put("GoodsType", type);
            jsonReq.put("start", start);
            jsonReq.put("num", num);
            jsonReq.put("GoodsId", goodsId);
            jsonReq.put("GoodsName", goodsName);
            storeServiceLog.info(jsonReq);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        new RequestHelper(ModelType.STORE).addRequest(jsonReq.toString())
                .addReceiver("goodsTable", new IOnRequestListener() {

                    @Override
                    public void onSuccess(String response) {
                        storeServiceLog.info(response);
                        try {
                            storeServiceLog.info(response);
                            JSONObject jRet = new JSONObject(response);
                            if(jRet.getInt("code") == 200) {
                                // 工作暂时到此, 要求每个请求均可以做到返回对象
                               JSONArray jsonArray = jRet.getJSONArray("GoodsTable");
                               for(int i=0;i<jsonArray.length();i++){
                                   storeServiceLog.info(jsonArray.get(i));
                               }
                               GoodsTable goodsTable = new GoodsTable();
                               goodsTable.setGoodsTable(jsonArray);
//                               updateView.action(evt, what)(goodsTable);
                               updateView.removeAllGoods(type);
                               for(int i=0;i<goodsTable.length();i++){
                                   updateView.addGoods(goodsTable.get(i));
                               }
                            } else {
                                // 错误处理
                                storeServiceLog.info(TAG + "ERROR");
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        
                    }
                }).build().start();
        return true;
    }

//	/* (non-Javadoc)
//	 * @see cn.melon.mod.store.biz.StoreService#getGoodsTableByType(java.lang.String)
//	 */
//	@Override
//	public GoodsTable getGoodsTableByType(String goodsType) {
//		
//		return null;
//	}
//
//	@Override
//	public GoodsTable getGoodsTableBySales(long goodsSales) {
//		
//		return null;
//	}
//
//	@Override
//	public Goods getGoodsById(String goodsId) {
//		
//		return null;
//	}
//
//	@Override
//	public GoodsTable getShoppingCart() {
//		
//		return null;
//	}
//
//	@Override
//	public GoodsTable getGoodsHistory() {
//		
//		return null;
//	}

}
